package com.woniuxy.profile.controller;

import com.woniuxy.commons.dto.BrowseDto;
import com.woniuxy.commons.dto.ScheduleDto;
import com.woniuxy.commons.entity.Browse;
import com.woniuxy.commons.entity.PageBean;
import com.woniuxy.commons.entity.ResponseEntity;
import com.woniuxy.commons.entity.Schedule;
import com.woniuxy.profile.service.ScheduleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Iterator;
import java.util.List;

/**
 * @Author: dengzj
 * @Description:
 * @Date: Created in 10:35 2020/7/30
 */
@RestController
@RequestMapping("/schedule")
public class ScheduleController {

    @Autowired
    private ScheduleService scheduleService;
    /*
    *@Author: dengzj
    *@Description: 学习进度查询
    *@Date: 10:41 2020/7/30
    */
    @RequestMapping("/findschedule")
    @ResponseBody
    public ResponseEntity<PageBean<Schedule>> findSchedule(@RequestBody ScheduleDto scheduleDto){
        String p=scheduleDto.getP();
        int page=1;
        if (p!=null){
            page=Integer.parseInt(p);
        }
        ResponseEntity<PageBean<Schedule>> entity=new ResponseEntity<>();
        PageBean<Schedule> schedule=scheduleService.findSchedule(scheduleDto,page);
        /*//遍历的到总时长，总积分
        Iterator<Schedule> scheduleIterator = schedule.getData().iterator();
        while (scheduleIterator.hasNext()) {
           int totaltime =Integer.parseInt(scheduleIterator.next().getStudytime());


        }*/
        if (schedule==null){
            entity.setCode(301).setMessage("未找到数据");
            return entity;
        }
        entity.setCode(200).setMessage("ok").setData(schedule);
        return entity;
    }
    /*
    *@Author: dengzj
    *@Description: 浏览记录
    *@Date: 11:40 2020/8/1
    */
    @RequestMapping("/browse")
    @ResponseBody
    public ResponseEntity<PageBean<Browse>> findBrowse(@RequestBody BrowseDto browseDto){
        String p=browseDto.getP();
        int page=1;
        if (p!=null){
            page=Integer.parseInt(p);
        }
        ResponseEntity<PageBean<Browse>> entity=new ResponseEntity<>();
        PageBean<Browse> browse=scheduleService.findBrowse(browseDto,page);
        if (browse==null){
            entity.setCode(301).setMessage("未找到数据");
            return entity;
        }
        entity.setCode(200).setMessage("ok").setData(browse);
        return entity;
    }
    /*
    *@Author: dengzj
    *@Description: 学习时长排名
    *@Date: 1:07 2020/8/4
    */
    @RequestMapping("/studyranking")
    @ResponseBody
    public ResponseEntity studyTime(@RequestBody ScheduleDto scheduleDto){
        ResponseEntity entity=new ResponseEntity();
        List<ScheduleDto> data=scheduleService.rankingOfStudyTime(scheduleDto);
        return entity.setData(data).setCode(200).setMessage("ok");
    }
}
